package com.yang;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.logging.Logger;

import static com.yang.IotDecoder.bytesToHex;


public class TcpServerHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = Logger.getLogger(TcpServerHandler.class.getName());

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
//        System.err.println(msg.getClass());
        byte[] msg1 = (byte[]) msg;
//        byte[] msg1 = (byte[]) msg;
//        byte[] data=new byte[msg1.readableBytes()];
//        msg1.readBytes(data);
        System.err.println("收到"+ bytesToHex(msg1));;
        ctx.channel().writeAndFlush(msg1);

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,
                                Throwable cause) throws Exception {
        cause.printStackTrace();
        logger.info("Unexpected exception from downstream.");
        ctx.close();
    }
}
